Skip to content

Conversation

@gsmlg
Copy link
Contributor

@gsmlg gsmlg commented Nov 3, 2025

Browser Fetch API Compatibility v0.7.0

🎯 Overview

This PR implements Browser Fetch API compatibility for the HTTP Fetch library, achieving ~85% API parity with JavaScript's Fetch API.

✨ Features Added

Response Properties (Browser API Standard)

  • status_text - Status message ("OK", "Not Found", etc.)
  • ok - Boolean for success status (200-299)
  • body_used - Body consumption tracking
  • redirected - Redirect detection
  • type - Response type (:basic, :cors, :error, :opaque)

Response Methods

  • Response.new/1 - Constructor with auto-populated Browser API fields
  • Response.clone/1 - Clone for multiple reads
  • Response.arrayBuffer/1 - Read as binary (ArrayBuffer equivalent)
  • Response.blob/1 - Read as Blob with metadata

New Modules

  • HTTP.StatusText - 60+ status code mappings
  • HTTP.Blob - Blob struct with data, type, and size

⚠️ Breaking Changes

Response Struct Fields Added

Five new fields: status_text, ok, body_used, redirected, type

Migration

Pattern matches need updating:

# Before
%Response{status: 200, headers: headers, body: body}

# After (ignore new fields)
%Response{status: 200, body: body} = response

📊 Test Coverage

41 tests, 0 failures (100% pass rate)

🔍 Files Changed

  • ✅ 8 files changed, 950 insertions(+), 31 deletions(-)
  • ✅ 3 new files: lib/http/blob.ex, lib/http/status_text.ex, tests
  • ✅ Updated: README, CHANGELOG, Response module, HTTP module, mix.exs

🎯 Browser API Usage Examples

response = HTTP.fetch("https://api.example.com/data") |> HTTP.Promise.await()

# Browser Fetch API properties
response.status        # 200
response.status_text   # "OK"
response.ok            # true
response.redirected    # false

# Clone for multiple reads
clone = HTTP.Response.clone(response)
json = HTTP.Response.json(response)
text = HTTP.Response.text(clone)

# Read as binary or Blob
binary = HTTP.Response.arrayBuffer(response)
blob = HTTP.Response.blob(response)

✅ Checklist

  • All tests passing (41/41)
  • Compilation successful
  • Documentation updated
  • Breaking changes documented
  • Semantic versioning commit

API Parity: ~85% Browser Fetch API compatibility 🎉

…ies and methods

BREAKING CHANGE: Response struct now includes Browser Fetch API fields

Added Browser Fetch API compatibility (~85% parity):
- Response properties: status_text, ok, body_used, redirected, type
- Response methods: clone(), arrayBuffer(), blob()
- HTTP.StatusText module for status code mapping
- HTTP.Blob module for binary data with metadata
- Response.new() constructor for consistent field population

Breaking changes:
- Response struct has 5 new fields requiring pattern match updates
- All Response construction now uses Response.new() internally

Features:
- Full Browser Fetch API Response interface
- Clone responses for multiple reads
- Read response as ArrayBuffer or Blob
- Automatic status text and ok field calculation
- 41 comprehensive tests covering all new features

Documentation:
- Browser Fetch API compatibility section in README
- Complete CHANGELOG with migration guide
- Documented Elixir-specific differences (immutability)

Ref: v0.7.0
- Add Credo inline directive for arrayBuffer camelCase (Browser API requirement)
- Fix alias ordering in test file (alphabetical)
- Change read_all spec from String.t() to binary() for correct type
- Fix text/1 function to pass full response struct to read_all
- All CI checks now passing: Credo, Dialyzer, Format, Compile
- Change doctests from struct pattern matching to field access
- Avoids partial struct matching issues with all required fields
- All response_test.exs doctests now pass (12 doctests, 0 failures)
- Change body type from String.t() to binary() (more accurate)
- Change url type to URI.t() | nil (can be nil)
- Change text/1 return type from String.t() to binary()
- All type specs now correctly reflect actual data types
- Dialyzer, Credo, and all tests passing
@gsmlg gsmlg merged commit d2db201 into main Nov 3, 2025
6 checks passed
@gsmlg gsmlg deleted the develop branch November 3, 2025 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants